package com.meichis.mcsappframework.utils;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Build;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.meichis.mcsappframework.BaseApplication;
import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.dialog.CustomMessageDialog;
import com.meichis.mcsappframework.dialog.MyAlertDialog;


public class MessageTools {

    public static void showShortToast(String pMsg) {
        if (pMsg != null) {
            if (Build.VERSION.SDK_INT >= 31) {
                showNestToast(BaseApplication.getContext(), pMsg, Toast.LENGTH_SHORT);
            } else {
                Toast.makeText(BaseApplication.getContext(), pMsg, Toast.LENGTH_SHORT).show();
            }
        }

    }

    public static void showLongToast(String pMsg) {
        if (pMsg != null) {
            if (Build.VERSION.SDK_INT >= 31) {
                showNestToast(BaseApplication.getContext(), pMsg, Toast.LENGTH_LONG);
            } else {
                Toast.makeText(BaseApplication.getContext(), pMsg, Toast.LENGTH_LONG).show();
            }
        }
    }

    public static void showNestToast(Context context, String pMsg) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_toast, null);
        TextView tvToast = view.findViewById(R.id.tv_toast);
        tvToast.setText(pMsg);
        Toast toast = new Toast(context.getApplicationContext());

        toast.setDuration(Toast.LENGTH_SHORT);
        //让Toast显示为我们自定义的样子
        toast.setView(view);
        toast.show();
    }

    public static void showNestToast(Context context, String pMsg, int show) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_toast, null);
        TextView tvToast = view.findViewById(R.id.tv_toast);
        tvToast.setText(pMsg);
        Toast toast = new Toast(context.getApplicationContext());

        toast.setDuration(show);
        //让Toast显示为我们自定义的样子
        toast.setView(view);
        toast.show();
    }

    public static void ShowmessageDialog(Context _context, String message) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        }).show();
    }

    public static void ShowmessageDialog(final Context _context, String message, final Intent intent) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                _context.startActivity(intent);
                return;
            }
        }).show();
    }

    public static void ShowmessageDialog01(Context _context, String message, OnClickListener sureclicklisnter) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();
    }


    public static void ShowBackmessageDialog(final Context _context, String message) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((Activity) _context).finish();
            }
        }).show();
    }

    public static void ShowmessageDialog(final Context _context, String title, String message) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setTitle(title).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        }).show();
    }

    public static void ShowmessageDialog(Context _context, String message, CustomMessageDialog.SureClickListener sureclicklisnter) {
        new CustomMessageDialog(_context, "提示", message, sureclicklisnter, new CustomMessageDialog.CancelClickListener() {
            @Override
            public void onClick() {

            }
        }).show();
    }

    public static void ShowReturnmessageDialog(final Context _context, String message, final Intent value, final int returncode) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((Activity) _context).setResult(returncode, value);
                ((Activity) _context).finish();
            }
        }).setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }

    public static void ShowFinishMessage(Context _context, String message) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SharePreferenceUtil.getInstance().remove(SharePreferenceUtil.PREFERENCES_AUTHKEY);
                ActivityUtils.getInstance().exit();
            }
        }).setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }

    public static void ShowmessageDialog(final Context _context, String title, String message, OnClickListener sureclicklisnter) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setTitle(title).setPositiveButton("确定", sureclicklisnter).show();
    }

    public static void ShowmessageDialog(Context _context, Spanned message, CustomMessageDialog.SureClickListener sureclicklisnter) {
        new CustomMessageDialog(_context, "提示", message, sureclicklisnter, new CustomMessageDialog.CancelClickListener() {
            @Override
            public void onClick() {

            }
        }).show();
    }

    public static void ShowmessageDialog(Context _context, Spanned message, CustomMessageDialog.SureClickListener sureclicklisnter, CustomMessageDialog.CancelClickListener cancelClickListener) {
        new CustomMessageDialog(_context, "提示", message, sureclicklisnter, cancelClickListener).show();
    }

    public static void ShowmessageDialog(Context _context, String message, CustomMessageDialog.SureClickListener sureclicklisnter, CustomMessageDialog.CancelClickListener cancelClickListener) {
        new CustomMessageDialog(_context, "提示", message, sureclicklisnter, cancelClickListener).show();
    }

    public static void ShowmessageDialog(Context _context, String message, String sureText, CustomMessageDialog.SureClickListener sureclicklisnter) {
        new CustomMessageDialog(_context, "提示", message, sureText, "", sureclicklisnter, null).show();
    }

    public static void ShowmessageDialog(Context _context, String message, String sureText, String cancelText, CustomMessageDialog.SureClickListener sureclicklisnter, CustomMessageDialog.CancelClickListener cancelClickListener) {
        new CustomMessageDialog(_context, "提示", message, sureText, cancelText, sureclicklisnter, cancelClickListener).show();
    }

    public static void ShowmessageDialog(Context _context, String message, boolean Cancelable, CustomMessageDialog.SureClickListener sureclicklisnter, CustomMessageDialog.CancelClickListener cancelClickListener) {
        new CustomMessageDialog(_context, "提示", message, Cancelable, sureclicklisnter, cancelClickListener).show();
    }

    public static void ShowmessageDialog(Context _context, String message, OnClickListener sureclicklisnter) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();
    }

    public static void ShowmessageDialog(Context _context, String message, OnClickListener sureclicklisnter, OnClickListener NegativeClick) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("取消", NegativeClick).setCancelable(false).show();
    }

    public static void ShowmessageSelectDialog(Context _context, String message, OnClickListener sureclicklisnter, OnClickListener NegativeClick) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("放弃", NegativeClick).show();
    }

    public static void ShowNeBackDialog(final Context _context, String message, OnClickListener sureclicklisnter) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((Activity) _context).finish();
            }
        }).setCancelable(false).show();
    }

    public static void ShowNeBackDialog(final Context _context, String message, OnClickListener sureclicklisnter, OnClickListener NegativeClick) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setNegativeButton("取消", NegativeClick).setCancelable(false).show();
    }

    public static void ShowmessageDialog001(Context _context, String message, OnClickListener sureclicklisnter) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("确定", sureclicklisnter).setCancelable(false).show();
    }

    public static void ShowmessageDialogYesOrNo(Context _context, String message, OnClickListener sureclicklisnter, OnClickListener NegativeClick) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("是", sureclicklisnter).setNegativeButton("否", NegativeClick).show();
    }

    public static void ShowTraceDialog(Context _context, String message, OnClickListener sureclicklisnter, OnClickListener NegativeClick) {
        new AlertDialog.Builder(_context, AlertDialog.THEME_HOLO_LIGHT).setMessage(message).setPositiveButton("直接确认", sureclicklisnter).setNegativeButton("继续扫码", NegativeClick).show();
    }

    public static void ShowmessageDialog(Context context, String message, MyAlertDialog.SureClickListener sureclicklisnter, MyAlertDialog.CancelClickListener negativeClick) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        new MyAlertDialog(context, "提示", message, sureclicklisnter, negativeClick).show();
    }

}


